﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.User;
using Enterprise.Data.User;
using Enterprise.Infrastructure;
using System.Data;
namespace Enterprise.Service.User
{
    /// <summary>
    /// 用户配置服务
    /// </summary>
    public class UserConfigService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-UserConfig";

        /// <summary>
        /// 人员列表 返回所有人员记录
        /// </summary>
        /// <returns></returns>
        public List<UserConfigEntity> UserConfigList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<UserConfigEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                UserConfigData data = new UserConfigData();
                List<UserConfigEntity> list = data.UserConfigList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void UserConfigExecute(UserConfigEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    //cmd = "Insert into sys_user(DEPTID,ULOGINNAME,ULOGINPASS,UNAME,USEX,UIP,UADMIN,USTATUS,UORDERBY) values(" + entity.DEPTID + ",'" + entity.ULOGINNAME + "','" + entity.ULOGINPASS + "','" + entity.UNAME + "','" + entity.USEX + "','" + entity.UIP + "','" + entity.UADMIN + "','" + entity.USTATUS + "'," + entity.UORDERBY + ")";
                    break;
                case "Update":
                    //cmd = "Update sys_user set DEPTID=" + entity.DEPTID + ", ULOGINNAME='" + entity.ULOGINNAME + "' , ULOGINPASS='" + entity.ULOGINPASS + "' , UNAME='" + entity.UNAME + "' , USEX='" + entity.USEX + "' , UIP='" + entity.UIP + "' , UADMIN='" + entity.UADMIN + "' , USTATUS='" + entity.USTATUS + "' , UORDERBY=" + entity.UORDERBY + " where USERID=" + entity.USERID;
                    break;
                case "Delete":
                    //cmd = "Delete from sys_user where USERID=" + entity.USERID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                UserConfigExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void UserConfigExecuteBySql(string cmd, int typeId)
        {
            UserConfigData data = new UserConfigData();
            data.UserConfigExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

    }
}
